/*******************************************************************************
 * Copyright 2011 WaTho
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package de.wathoserver.android.dragoid.activity.util;

import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import de.wathoserver.android.dragoid.R;
import de.wathoserver.android.dragoid.api.DragoidConstants;
import de.wathoserver.android.dragoid.util.LogCollector;

/**
 * Aktivität zum Anzeigen und Versenden von Logginginfos.
 * 
 * @author watho
 * 
 */
public class ShowLogActivity extends Activity implements DragoidConstants {
	private TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showlogview);
		tv = (TextView) findViewById(R.id.showlog);
		tv.setText(getIntent().getStringExtra("log"));
		tv.setClickable(false);
		Pattern dragoidMatcher = Pattern.compile("Dragoid");
		Linkify.addLinks(tv, dragoidMatcher, "http://");
		tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(LOG_TAG, "Clicked");
			}
		});
	}

	/**
	 * Wird von xml aufgerufen.
	 * 
	 * @param btn
	 */
	public void send(View btn) {
		// Uri emailUri = Uri.parse("mailto:wathosoft@gmx.net");
		StringBuilder sb = new StringBuilder()
				.append(LogCollector.LINE_SEPARATOR);
		String phoneInfo = LogCollector.collectPhoneInfo();
		sb.append(LogCollector.LINE_SEPARATOR).append(phoneInfo);
		sb.append(tv.getText());
		String content = sb.toString();
		Intent intent = new Intent(Intent.ACTION_SEND);
		// intent.setType("text/plain");
		intent.setType("message/rfc822");
		intent.putExtra(Intent.EXTRA_EMAIL,
				new String[] { "wathosoft@gmx.net" });
		intent.putExtra(Intent.EXTRA_SUBJECT, "Dragoid-Log");
		intent.putExtra(Intent.EXTRA_TEXT, content);
		this.startActivity(Intent.createChooser(intent,
				"Wähle Emailanwendung aus"));
	}
}
